<template>
  <div>
    <!--面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域 -->
    <el-card>
      <!--搜索与添加区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button @click="getOrderList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--表格区域 -->
      <el-table border stripe :data="orderList">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column width="135px" label="订单价格" prop="order_price"></el-table-column>
        <el-table-column width="110px" label="是否付款" prop="pay_status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '0'" type="danger">未付款</el-tag>
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="110px" label="是否发货" prop="is_send"></el-table-column>
        <el-table-column width="180px" label="下单时间">
          <template slot-scope="scope">
            {{scope.row.create_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column width="170px" label="操作">
          <template>
            <el-button @click="showEidtDialog" size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="showProgresBox" size="mini" type="success" icon="el-icon-location"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--修改对话框区域 -->
    <el-dialog
      @close="resetForm"
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%">
      <!--修改表单区域 -->
      <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            expand-trigger="hover"
            v-model="addressForm.address1"
            :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addressVisible = false">取 消</el-button>
    <el-button type="primary" @click="addressVisible = false">确 定</el-button>
  </span>
    </el-dialog>
    <!--显示物流信息对话框区域 -->
    <el-dialog
      title="物流信息"
      :visible.sync="progressVisible"
      width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'

export default {
  created () {
    this.getOrderList()
  },
  data: function () {
    return {
      // 查询订单的参数条件
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 数据总条数
      total: 0,
      // 订单数据数组
      orderList: [],
      // 控制修改对话框的显示与隐藏
      addressVisible: false,
      // 修改表单的数据
      addressForm: {
        address1: [],
        address2: ''
      },
      // 修改地址表单数据的校验规则对象
      addressFormRules: {
        address1: [
          {
            required: true,
            message: '请选择省市区/县',
            trigger: 'blur'
          }
        ],
        address2: [
          {
            required: true,
            message: '请输入详细地址',
            trigger: 'blur'
          }
        ]
      },
      cityData,
      // 控制物流信息对话框的显示与隐藏
      progressVisible: false,
      // 物流信息数据
      progressInfo: []
    }
  },
  methods: {
    // 获取所有订单列表数据
    getOrderList: async function () {
      var { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取订单数据失败！')
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听当前页码改变事件
    handleCurrentChange: function (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 监听分页条数改变事件
    handleSizeChange: function (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 点击按钮显示修改对话框
    showEidtDialog: function () {
      this.addressVisible = true
    },
    // 关闭对话框，重置表单
    resetForm: function () {
      this.$refs.addressFormRef.resetFields()
    },
    // 点击按钮显示物流信息对话框
    showProgresBox: async function () {
      var { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error('获取物流信息失败！')
      this.progressInfo = res.data
      this.progressVisible = true
      console.log(this.progressInfo)
    }
  }
}
</script>

<style lang="less" scoped>
  @import "../../plugins/timeline/timeline.css";
  @import "../../plugins/timeline-item/timeline-item.css";
  .el-pagination {
    display: flex;
    justify-content: center;
  }

  .el-cascader {
    width: 100%;
  }
</style>
